<?php if(!defined('WDK_PATH')) exit('No direct script access allowed');
class Hp_Curl
{
	private $url; ///< 접속 URL
	private $cookie; ///< 쿠키파일
	private $method; ///< post 값 여부
	private $params; ///< 전송할 파라미터
	private $params_type; ///< 전송할 파라미터 타입
	private $return; ///< Curl 옵션
	private $timeout; ///< Curl 옵션
	private $addopt; ///< 추가 Curl 옵션
	private $response; 
	private $header; ///< 결과 헤더
	private $result; ///< 결과값
	private $server_lang; ///< 서버 스크립트 언어
	public static $LANG_PHP	=	1;
	public static $LANG_ASP	=	2;
	public static $LANG_JSP	=	3;
	
	function __construct()
	{
		$this->addopt = array();
		$this->params = array();
		$this->set_url(null);
		$this->set_cookie(null);
		$this->set_method('GET');
		$this->set_params_type('GET');
		$this->set_timeout(30);
		$this->set_return(1);
		$this->set_server_lang(self::$LANG_PHP);
		$this->header = null;
		$this->result = null;
	}
	
	/**
		@date		2010-06-17
		@author 	DongHun Kim
		@param		 	
		@return		 	
		@brief		Http 클라이언트 
	*/
	public function set_url($url)				{	$this->url			=	$url;}
	public function set_cookie($cookie)			{	$this->cookie		=	$cookie;}
	public function set_method($method)			{	$this->method		=	$method;}
	public function set_params_type($params_type){	$this->params_type	=	$params_type;}
	public function set_timeout($timeout)		{	$this->timeout		=	$timeout;}
	public function set_return($return)			{	$this->return		=	$return;}
	public function set_server_lang($lang)		{	$this->server_lang	=	$lang;}
	public function set_add_opt($key='', $value='')
	{
		if(trim($key) == '') return;
		$this->addopt[$key] = $value;
	}
	public function set_params($key='', $value='')
	{
		if(trim($key) == '') return;
		$this->params[$key] = $value;
	}
	
	public function get_result()
	{
		return $this->result;
	}
	public function get_header()
	{
		return $this->header;
	}
	public function get_cookies()
	{
		if($this->header != null)
		{
			$arr = explode("\n", $this->header);
			$cookie = array();
			while($item = array_shift($arr))
			{
				if(strpos($item, 'Set-Cookie') === 0)
				{
					$cookie[] = trim(substr($item, strlen('Set-Cookie:')));
				}
			}
			return $cookie;
		}
		return;
	}
	public function get_location()
	{
		if($this->header != null)
		{
			$arr = explode("\n", $this->header);
			$cookie = array();
			while($item = array_shift($arr))
			{
				if(strpos($item, 'Location') === 0)
				{
					return (substr($item, strlen('Location:')));
				}
			}
		}
		return;
	}
	
	function exec()
	{
		// Curl 실행
		$ch = curl_init();
		if(strtoupper(trim($this->method)) == 'POST')
		{
			curl_setopt ($ch, CURLOPT_POST, 1);
		}
		
		if(strtoupper($this->params_type) == 'GET')
		{ 
			if(is_array($this->params) && sizeof($this->params) > 0)
			{ 
				switch ($this->server_lang)
				{
					case self::$LANG_ASP :
					case self::$LANG_JSP :
						$query = "";
						foreach($this->params as $key=>$param)
						{
							if(strlen($query) > 0) $query.= "&";
							if(is_array($param) && count($param) > 0)
							{
								foreach($param as $val)
								{
									$query.= $key.'='.urlencode($val).'&';
								}
								$query = substr($query, 0, -1);
							}
							else
							{
								$query.= $key.'='.urlencode($param);
							}
						}
						$this->params = $query;
					break;
					case self::$LANG_PHP :
					default:
						$this->params = http_build_query($this->params);
					break;
				}
			}
		}
		curl_setopt ($ch, CURLOPT_POSTFIELDS, $this->params);
		curl_setopt ($ch, CURLOPT_URL,$this->url);		
		curl_setopt ($ch, CURLOPT_TIMEOUT, $this->timeout); 
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, $this->return);
		if($this->cookie != null && strlen($this->cookie) > 0)
		{
			if(!file_exists($this->cookie))
			{
				file_put_contents($this->cookie, '');
			}
			curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookie); 
			curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookie);	
		}		
		// 추가 Curl옵션이 있을경우 이를 적용 시켜줌 
		if(is_array($this->addopt) && count($this->addopt) > 0)
		{
			curl_setopt_array($ch, $this->addopt); 
		}
		$this->response = curl_exec($ch);
		$this->parse_result();
		curl_close($ch);
	}
	private function parse_result()
	{
		if(isset($this->addopt[CURLOPT_HEADER]) && $this->addopt[CURLOPT_HEADER])
		{
			$arr = explode("\n", $this->response);
			$buffer = array();
			while($line = array_shift($arr))
			{
				$buffer[] = $line;
				if(strpos($line, "Content-Type:") === 0) break;
			}
			array_shift($arr);
			$this->header = implode("\n", $buffer);
			$this->result = implode("\n", $arr);
			return;
		}
		$this->result = $this->response;
	}
}